package qlsl.androiddesign.view.subview.activityview;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.qlsl.androiddesign.kuaiwa.R;

import java.util.ArrayList;
import java.util.List;

import qlsl.androiddesign.activity.subactivity.SearchActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingDetailActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingListActivity;
import qlsl.androiddesign.adapter.subadapter.ShoppingListNewAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.entity.otherentity.ThreeCategory.RowsBean;
import qlsl.androiddesign.http.service.subservice.ShoppingListService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshListView;
import qlsl.androiddesign.listener.OnPullRefreshListener;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 商品列表页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class ShoppingListViewOld extends FunctionView<ShoppingListActivity> {

	private PullToRefreshListView refreshView;

	private List<ShoppingDetail> list = new ArrayList<ShoppingDetail>();

	private Pager pager;

	public ShoppingListViewOld(ShoppingListActivity activity) {
		super(activity);
		setContentView(R.layout.activity_shopping_listview);

	}

	@SuppressLint("ResourceAsColor")
	protected void initView(View view) {
		setTitle("商品列表");
		refreshView = findViewById(R.id.refreshListView);
		RowsBean item = getItem();
		if (item.flage == 1) {
			setRightButtonResource(R.drawable.czhao);
			showRightButton();
		}
		getShoppingList(item, "0");
	}

	protected void initData() {

	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
	}

	public <T> void showData(T... t) {
		if (t[0] instanceof List) {
			List<ShoppingDetail> list_result = (List<ShoppingDetail>) t[0];
			resetList(list_result);
			notifyDataSetChanged();
		}

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.defaults:// 默认
			doClickDefaults("0", view);
			break;
		case R.id.volumes_rela:// 销量
			doClickDefaults("1", view);
			break;
		case R.id.prives_rela:// 价格
			doClickDefaults("2", view);
			break;
		case R.id.list_item:
			doClickListItem(view);
			break;
		case R.id.btn_right:
			startActivity(SearchActivity.class);
			break;

		}
	}

	private RowsBean getItem() {
		RowsBean item = (RowsBean) activity.getIntent()
				.getSerializableExtra("data");

		return item;
	}

	private void resetList(List<ShoppingDetail> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new ShoppingListNewAdapter(activity, list);
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	protected void getShoppingList(RowsBean item, String orderby) {
		ShoppingListService.queryShoopingList(null, item.getSumcierid(),
				orderby, this, activity);
	}

	protected void doClickDefaults(String type, View view) {
		ViewGroup parent = (ViewGroup) view.getParent();// 获取点击这个控件的父控件
		int position = parent.indexOfChild(view);// 获取点击控件在父控件的位置
		for (int index = 0; index < parent.getChildCount(); index++) {
			View childView = parent.getChildAt(index);// 获取子控件
			if (position == index) {
				childView.setActivated(true);
			} else {
				childView.setActivated(false);
			}
		}

		ShoppingListService.queryShoopingList(null, getItem().getSumcierid(),
				type, this, activity);
	}

	protected void doClickListItem(View view) {
		int pos = refreshView.getRefreshableView().getPositionForView(view);
		ShoppingDetail item = (ShoppingDetail) refreshView.getAdapter()
				.getItem(pos);
		if (item != null) {
			startActivity(item, ShoppingDetailActivity.class);
		}
	}

	private OnPullRefreshListener<ListView> refreshListener = new OnPullRefreshListener<ListView>() {

		public void onPullDown() {
			getShoppingList(getItem(), "0");
		}

		public void onPullUp() {
			super.onPullUp(refreshView, pager);
		}

		public void nextPager() {
			getShoppingList(getItem(), "0");
		}
	};

}
